package com.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.pojo.Course;
import com.tools.SomeMonitorEditText;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class AddCourseActivity extends AppCompatActivity {

    private ImageButton 后退按钮;
    private TextView 标题;
    private EditText 编辑课程名称;
    private EditText 编辑任课老师;
    private EditText 编辑开课年;
    private EditText 编辑开课月;
    private EditText 编辑开课日;
    private EditText 编辑结课年;
    private EditText 编辑结课月;
    private EditText 编辑结课日;
    private Button 提交按钮;


    String courseName;
    String courseTeacher;
    String startDate;
    String endDate;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_course);

        后退按钮 = (ImageButton) findViewById(R.id.后退按钮);
        标题 = (TextView) findViewById(R.id.标题);
        编辑课程名称 = (EditText) findViewById(R.id.编辑课程名称);
        编辑任课老师 = (EditText) findViewById(R.id.编辑任课老师);
        编辑开课年 = (EditText) findViewById(R.id.编辑开课年);
        编辑开课月 = (EditText) findViewById(R.id.编辑开课月);
        编辑开课日 = (EditText) findViewById(R.id.编辑开课日);
        编辑结课年 = (EditText) findViewById(R.id.编辑结课年);
        编辑结课月 = (EditText) findViewById(R.id.编辑结课月);
        编辑结课日 = (EditText) findViewById(R.id.编辑结课日);
        提交按钮 = (Button) findViewById(R.id.提交按钮);

        后退按钮.setColorFilter(Color.BLACK);

        后退按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AddCourseActivity.this.finish();
                Intent intent = new Intent();
                intent.setClass(AddCourseActivity.this, SetCourseActivity.class);
                startActivity(intent);
            }
        });

        //没写完无法提交
        new SomeMonitorEditText().SetMonitorEditText(提交按钮,编辑课程名称,编辑任课老师,编辑开课年,编辑开课月,编辑开课日,编辑结课年,编辑结课月,编辑结课日);

        标题.setText("编辑课程");
        标题.setTextColor(Color.BLACK);



        提交按钮.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                courseName = 编辑课程名称.getText().toString().trim();
                courseTeacher = 编辑任课老师.getText().toString().trim();
                startDate = 编辑开课年.getText().toString().trim()+"-"+编辑开课月.getText().toString().trim()+"-"+编辑开课日.getText().toString().trim();
                endDate = 编辑结课年.getText().toString().trim()+"-"+编辑结课月.getText().toString().trim()+"-"+编辑结课日.getText().toString().trim();

                String path = "http://101.200.40.47:8080/stu/course/addCourse?courseName="+courseName+
                        "&courseTeacher="+courseTeacher+
                        "&startDate="+startDate+
                        "&endDate="+endDate;

                new AddCourse().execute(path);
            }
        });



    }

    //编辑课程
    private class  AddCourse extends AsyncTask {

        @Override
        protected Object doInBackground(Object[] params) {

            String path = params[0].toString();

            try {
                //以get方式提交整个路径
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");//这个应该都知道是干嘛的，默认是GET  
                conn.setConnectTimeout(5000);

                if (conn.getResponseCode() == 200) {
                    InputStream is = conn.getInputStream();
                    BufferedReader br = new BufferedReader(new InputStreamReader(is));
                    String str = br.readLine();
                    return str;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            String info = o.toString().trim();
            if(info.equals("1")){
                Toast.makeText(AddCourseActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
                AddCourseActivity.this.finish();
                Intent intent = new Intent();
                intent.setClass(AddCourseActivity.this, SetCourseActivity.class);
                startActivity(intent);
            }else if((info.equals("0"))){
                Toast.makeText(AddCourseActivity.this, "该课程已存在！请修改课程名！", Toast.LENGTH_SHORT).show();
                编辑课程名称.setText(null);
            }else{
                Toast.makeText(AddCourseActivity.this, "未知错误！修改失败！", Toast.LENGTH_SHORT).show();
            }
        }
    }

    long mExitTime;       //实现“再按一次退出”的记录时间变量
    @Override //再按一次退出程序
    public void onBackPressed() {
        if (System.currentTimeMillis() - mExitTime < 2000) {
            super.onBackPressed();
        } else {
            mExitTime = System.currentTimeMillis();
            Toast.makeText(AddCourseActivity.this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
        }
    }
}
